package zld369504.tw.m_team.fragments;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.LinkedHashMap;
import java.util.Map;

import zld369504.tw.m_team.MySharedPreference;
import zld369504.tw.m_team.R;
import zld369504.tw.m_team.dao.TorrentDetail;

public class TorrentBasicInfoFragment extends BaseFragment implements View.OnClickListener {

    private View rootView;
    private TableLayout tableLayout;
    private Button btnIpv4, btnIpv6;

    private TorrentDetail detail;

    public static TorrentBasicInfoFragment getInstance(TorrentDetail detail) {
        TorrentBasicInfoFragment fragment = new TorrentBasicInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("detail", detail);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            detail = (TorrentDetail) bundle.getSerializable("detail");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_torrent_basic_info, container, false);
        tableLayout = rootView.findViewById(R.id.basicInfoTableLayout);
        btnIpv4 = rootView.findViewById(R.id.btn_ipv4);
        btnIpv6 = rootView.findViewById(R.id.btn_ipv6);
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        btnIpv4.setOnClickListener(this);
        btnIpv6.setOnClickListener(this);
        if (detail != null) {
            LinkedHashMap<String, String> map = detail.getBasicInfo();
            for (Map.Entry<String, String> element : map.entrySet()) {
                String strKey = element.getKey();
                String strObj = element.getValue();
                TableRow tableRow = new TableRow(getActivity());
                TextView textViewKey = new TextView(getActivity());
                textViewKey.setText(strKey);
                TextView textViewValue = new TextView(getActivity());
                textViewValue.setText(strObj);
                tableRow.addView(textViewKey);
                tableRow.addView(textViewValue);
                tableLayout.addView(tableRow);
            }
        }
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        switch (viewId) {
            case R.id.btn_ipv4:
                copyTextToClipboard(detail.getIpv4HttpsTorrentUrl());
                break;
            case R.id.btn_ipv6:
                copyTextToClipboard(detail.getIpv6HttpsTorrentUrl());
                break;
        }
    }

    private void copyTextToClipboard(final String text) {
        if (getActivity() != null) {
            final MySharedPreference mySharedPreference = MySharedPreference.getInstance();
            if (!mySharedPreference.getCopyTorrentLinkHintReadState(getActivity())) {
                new AlertDialog.Builder(getActivity())
                        .setMessage(getResources().getString(R.string.copy_t_link_first_time_hint))
                        .setPositiveButton(getResources().getString(R.string.Continue), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mySharedPreference.markCopyTorrentLinkHintRead(getActivity(), true);
                                copy(text);
                            }
                        }).show();
            } else {
                copy(text);
            }
        } else {
            //cpoy failed
            showErrorDialog(getResources().getString(R.string.copy_failed));
            Log.d("ClipboardManager", "cpoy failed.getActivity()=null");
        }
    }

    private void copy(String text) {
        ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
        assert cm != null;
        ClipData clipData = ClipData.newPlainText(null, text);
        cm.setPrimaryClip(clipData);
        Toast.makeText(getActivity(), getResources().getString(R.string.copy_success), Toast.LENGTH_SHORT).show();
    }
}
